<?php 
/*
 * 续借接口
 */
namespace Home\Controller;
use Think\Controller;
use Common\Library\BusinessException;
use Common\Library\Constant;
use Common\Library\Http\Request;
use Common\Library\Http\Response;
use Think\Log;
use Common\Vendor\Hook\BaseHook;
use Common\Library\SendSMS;

class RenewController extends Controller {
       
    public function __construct() {
        parent::__construct();
    
        try {
            Request::parse();
        } catch (BusinessException $e) {
            $this->ajaxReturn(['code' => $e->getCode(), 'msg' => $e->getMessage()]);
        }
    }
    
    public function RenewByUser(){
           try {
               $customerId = I('post.customer_id', 0, 'int');
               if ($customerId == 0){
                   $ret['code'] = 0;
                   $ret['msg'] = "成功";
                   $ret['data'] = array('customer_id'=>$customerId,'status'=>"-1",);
               }else {
                   
               $CustomerBankInfo = M('Customer');
               $userinfo = D('Customer')->has($customerId);
               
               if (!$userinfo) {
                   BusinessException::throwException(Constant::INVALID_PARAMETER);
               }
               
               $where['customer_id'] = $customerId;
               $consumestatus = M('consumeHistory')->where($where)->order('add_time desc')->field('id,repayment_time,status')->find();
               
               $data1 = date("Y-m-d,H:i:s",time());
               $data2 = date("Y-m-d,H:i:s",$consumestatus['repayment_time']);
               $date=floor((strtotime($data1)-strtotime($data2))/86400);
               
			   
                   if(abs($date) <= 3 && $consumestatus['status'] != 3){
                       $ret['code'] = 0;
                       $ret['msg'] = "成功";
                       $ret['data'] = array('customer_id'=>$customerId,'status'=>14,'orderId'=>$consumestatus['id']);
                   }else {
                       $ret['code'] = 0;
                       $ret['msg'] = "成功";
                       $ret['data'] = array('customer_id'=>$customerId,'status'=>$consumestatus['status'],'orderId'=>$consumestatus['id']);
                       //$ret['data'] = array('customer_id'=>$customerId,'status'=>14,'orderId'=>$consumestatus['id']);
                   }
               } 
           }catch (BusinessException $e) {
            $this->ajaxReturn(['code' => $e->getCode(), 'msg' => $e->getMessage()]);
        }

        Response::format($ret);
        $this->ajaxReturn($ret);
        
    }
    
    
    public function GetConsumeInfoById(){
        try {
            $customerId = I('post.customer_id', 0, 'int');
            $consumeId = I('post.consume_id', 0, 'int');
            $repayId = I('post.repay_id', 0, 'int');
            if($repayId == 0){
                Log::write("repayId : ".$repayId, "老版本过来没有repay_id", Log::DEBUG);
            }else{
                //新版本是有repay_id, 根据这个id去取出产品得到天数
                $cash_conf = M('SysInstallmentPlan')->where(['publish' => 1, 'target' => 3, 'id'=>$repayId])->select();
                Log::write(json_encode($cash_conf), "新版本取出repay_id=".$repayId."产品", Log::DEBUG);
            }

            if(!$customerId && !$consumeId){
                BusinessException::throwException(Constant::INVALID_PARAMETER);
            }
            
            $where['id'] = $consumeId;
            $where['customer_id'] = $customerId;
            $consumestatus = M('consumeHistory')->order('add_time DESC')->where($where)->limit(1)->select();
            
            if($consumestatus[0]['amount'] > 200000){
                $ret['code'] = 1;
                $ret['msg'] = "申请额度不能大于2000";
            }else {
                $ret['code'] = 0;
                $ret['msg'] = "成功";
                if(!empty($cash_conf)){
                    //新版本，需要根据repay_id找到这个产品，给出天数
                    $chooseProduct = $cash_conf[0];
                    $chooseP = D('Finish')->getWithdrawalDetail($consumestatus[0]['amount'], $chooseProduct, $chooseProduct['repay_unit']);
                    $ret['data'] = array('customer_id'=>$customerId,'consume_id'=>$consumeId,'data'=>$consumestatus, "product"=>$chooseP);
                }else{
                    //老版本, 支持一个产品的情况，前端写死了30天
                    $ret['data'] = array('customer_id'=>$customerId,'consume_id'=>$consumeId,'data'=>$consumestatus);
                }

            }
            
        }catch (BusinessException $e) {
            $this->ajaxReturn(['code' => $e->getCode(), 'msg' => $e->getMessage()]);
        }
    
        Response::format($ret);
        $this->ajaxReturn($ret);
    
    }
}
